'use strict';

const createError = require('./createError');

/**
 * 根据响应结果执行Promise.resolve或者Promise.reject
 *
 * @param {Function} resolve Promise.resolve函数.
 * @param {Function} reject Promise.reject函数.
 * @param {object} response 响应对象
 */
module.exports = function settle(resolve, reject, response) {
  const validateStatus = response.config.validateStatus;
  if (!response.status || !validateStatus || validateStatus(response.status)) {
    resolve(response);
  } else {
    reject(createError(
      'Request failed with status code ' + response.status,
      response.config,
      null,
      response.request,
      response
    ));
  }
};
